<?php

namespace Biz\cli\IniData;

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShopFavorite;
use Biz\model\GoodsActionCount;
use Medoo\Medoo;
use Biz\model\trade\Cart;
use Biz\model\trade\CartV;
use Biz\model\trade\OrderGoods;

class GoodsActionCounterFun {

	public static function InitData($dateRange) {
		////////////// 搜藏数统计
		$infos = ShopFavorite::select([
			'obj_id',
			'action_num' => Medoo::raw("count('obj_id')")
		], [
			'obj_type' => ShopFavorite::TYPE_GOODS,
			'add_time[>]' => $dateRange[0],
			'add_time[<]' => $dateRange[1],
			'GROUP' => 'obj_id'
		]);
		foreach ($infos as $info) {
			if (!GoodsActionCount::get('*', ['goods_id' => $info['obj_id'], 'type' => GoodsActionCount::DEGREE_TYPE_FAV, 'add_date' => $dateRange[0]])) {
				GoodsActionCount::insert([
					'goods_id' => $info['obj_id'],
					'type' => GoodsActionCount::DEGREE_TYPE_FAV,
					'action_num' => $info['action_num'],
					'add_date' => $dateRange[0],
					'status' => GoodsActionCount::STAUTS_NORMAL,
				]);
			}
		}

		/////////////  被加车
		$infos = Cart::select([
			'goods_id',
			'action_num' => Medoo::raw("count('goods_id')")
		], [
			'status' => [Cart::STATUS_ACTIVE, Cart::STATUS_FAIL],
			'addtime[>]' => $dateRange[0],
			'addtime[<]' => $dateRange[1],
			'GROUP' => 'goods_id'
		]);

		$vInfos = CartV::select([
			'goods_id',
			'action_num' => Medoo::raw("count('goods_id')")
		], [
			'status' => [CartV::STATUS_ACTIVE, CartV::STATUS_FAIL],
			'addtime[>]' => $dateRange[0],
			'addtime[<]' => $dateRange[1],
			'GROUP' => 'goods_id'
		]);

		$allinfos = [];
		foreach ($infos as $k => $info) {
			foreach ($vInfos as $vk => $vInfo) {
				if ($info['goods_id'] == $vInfo['goods_id']) {
					$allinfos[] = [
						'goods_id' => $info['goods_id'],
						'action_num' => $info['action_num'] + $vInfo['action_num'],
					];
					unset($infos[$k]);
					unset($vInfos[$vk]);
				}
			}
		}

		$allinfos = array_merge($allinfos, $infos, $vInfos);
		foreach ($allinfos as $allinfo) {
			if (!GoodsActionCount::get('*', ['goods_id' => $info['goods_id'], 'type' => GoodsActionCount::DEGREE_TYPE_ADD_CART, 'add_date' => $dateRange[0]])) {
				GoodsActionCount::insert([
					'goods_id' => $allinfo['goods_id'],
					'type' => GoodsActionCount::DEGREE_TYPE_ADD_CART,
					'action_num' => $info['action_num'],
					'add_date' => $dateRange[0],
					'status' => GoodsActionCount::STAUTS_NORMAL,
				]);
			}
		}

		/////////////  被下单

		$infos = OrderGoods::select([
			'goods_id',
			'action_num' => Medoo::raw("count('goods_id')")
		], [
			'addtime[>]' => $dateRange[0],
			'addtime[<]' => $dateRange[1],
			'GROUP' => 'goods_id'
		]);
		foreach ($infos as $info) {
			if (!GoodsActionCount::get('*', ['goods_id' => $info['goods_id'], 'type' => GoodsActionCount::DEGREE_TYPE_ORDER, 'add_date' => $dateRange[0]])) {
				GoodsActionCount::insert([
					'goods_id' => $info['goods_id'],
					'type' => GoodsActionCount::DEGREE_TYPE_ORDER,
					'action_num' => $info['action_num'],
					'add_date' => $dateRange[0],
					'status' => GoodsActionCount::STAUTS_NORMAL,
				]);
			}
		}
	}

}
